var editMenu=null;
var editName=null;
var editLink=null;
var editProjectId=null;
var editID=null;
var editSelect=null;
var editMenuNormalHeight=0;
var editMenuWithIssue=0;
var singleAddPopup=null;
var editIssuePopup=null;
var payButton=null;
var editRelease=null;
var deliverInfo=null;
function init() {
	editMenu=document.getElementById("editMenu");
	editMenuNormalHeight=editMenu.offsetHeight-10;
	editName=document.getElementById("editName");
	editLink=document.getElementById("editLink");
	editProjectId=document.getElementById("editProjectId");
	editID=document.getElementById("editID");
	editSelect=document.getElementById("editSelect");
	editRelease=document.getElementById("editRelease");
	singleAddPopup=document.getElementById("singleAdd");
	editIssuePopup=document.getElementById("editIssue");
	payButton=document.getElementById("pay_button");
	var issueEditor=document.getElementById("issueEditor"); 
	issueEditor.style.display="block";
	editMenuWithIssue=editMenuNormalHeight+issueEditor.offsetHeight;
	editMenu.style.height=editMenuNormalHeight+"px";
	//deliverInfo
	deliverInfo=document.getElementById("deliverInfo");
}

function showEditButton(o) {
	o.childNodes[3].style.opacity=1;
}
function hideEditButton(o) {
	o.childNodes[3].style.opacity=0;
}

function closePopup(popup){
	popup.style.opacity=0;
	setTimeout(function(){
		popup.style.visibility="hidden";
	},1000);
}

function showPopup(popup){
	if(popup.style.visibility!="visible"){
		popup.style.visibility="visible";
		popup.style.opacity=1;
	}
}

function showEditMenu(o, id, userId, event) {
	showPopup(editMenu);
	editMenu.style.left=event.pageX+"px";
	if((event.pageY+editMenu.offsetHeight)<document.getElementById("content").offsetHeight){
		editMenu.style.top=event.pageY+"px";
	}else{
		editMenu.style.top=(event.pageY-editMenu.offsetHeight)+"px";
	}
	var taskNameLink=document.getElementById("name_link_"+id);
	editName.value=taskNameLink.innerHTML;
	editLink.value=taskNameLink;
	editProjectId.value=document.getElementById("projectId_"+id).innerHTML;
	editID.value=id;
	editRelease.value=document.getElementById("release_"+id).innerHTML;
	
	for(var i=0; i<editSelect.options.length; i++){
		if(editSelect.options[i].value==userId){
			if(editSelect.selectedIndex!=i){
				editSelect.selectedIndex=i;
			}		
		}
	}
	
	//hide pay button if already paid
	if(document.getElementById("pay_"+id).innerHTML.indexOf("img",0)>-1){
		payButton.style.visibility="hidden";
	}else{
		payButton.style.visibility="visible";
	}
}

//ajax
var lastImg;
function changeStatus(o, step, id, event){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.status==200){
				if(request.responseText=="no_rights") return;
				var img=o.childNodes[1];
				if(request.responseText!=("no")&&request.responseText!=("yes")){
					if((event.pageY+editIssuePopup.offsetHeight)<document.getElementById("content").offsetHeight){
						editIssuePopup.style.top=event.pageY+"px";
					}else{
						editIssuePopup.style.top=(event.pageY-editIssuePopup.offsetHeight)+"px";
					}
					editIssuePopup.style.left=(event.pageX-editIssuePopup.offsetWidth)+"px";
					lastImg=img;
					document.getElementById("issueContent").value=request.responseText;
					editIssuePopup.dataId=id;
					editIssuePopup.dataName=step;
					showPopup(editIssuePopup);
				}else{
					img.src="css/"+request.responseText+".png";
				}
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=UPDATE_TASK_STATUS&name="+step+"&id="+id,true);
	request.send();
}


function updateTaskData(){
	closePopup(editMenu);
	var userId=editSelect.options[editSelect.selectedIndex];
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText=="success"){
				var projectId=document.getElementById("projectId_"+editID.value);
				projectId.innerHTML=editProjectId.value;
				projectId.href="http://webeeh.gdev.com/projects/issue_tracker_projects/Project%20"+editProjectId.value
									+"?object_id="+editProjectId.value+"&project_type=Project";
				var release=document.getElementById("release_"+editID.value);
				release.innerHTML=editRelease.value;
				release.href="http://webeeh.gdev.com/projects/issue_tracker_projects/Release%20"+editRelease.value+
								"?object_id="+editRelease.value+"&project_type=Release";
				document.getElementById("userID_"+editID.value).innerHTML=userId.innerHTML;
				document.getElementById("name_link_"+editID.value).href=editLink.value;
				document.getElementById("name_link_"+editID.value).innerHTML=editName.value;
			}else{
				alert("internal server error");
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=UPDATE_TASK&id="+editID.value+
			"&name="+editName.value+"&link="+editLink.value+"&account_id="+
			userId.value+"&project_id="+editProjectId.value+"&release="+editRelease.value,true);
	request.send(); 
}

function deleteTaskData(){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText=="error"){
				alert("internal server error");
			}else{
				alert("deleted");
				document.location.reload(true);
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=DELETE_TASK&id="+editID.value,true);
	request.send();
	closePopup(editMenu);
}


var issueEditorFlag=true;
function showIssueEditor(){
	if((editMenu.offsetTop+editMenuWithIssue)<
			document.getElementById("content").offsetHeight){
		editMenu.style.height=editMenuWithIssue+"px";
		issueEditorFlag=true;
	}else{
		editMenu.style.top=(editMenu.offsetTop-editMenuNormalHeight)+"px";
		editMenu.style.height=editMenuWithIssue+"px";
		issueEditorFlag=false;
	}
}

function hideIssueEditor(){
	if(issueEditorFlag){
		editMenu.style.height=editMenuNormalHeight+"px";
	}else{
		editMenu.style.height=editMenuNormalHeight+"px";
		editMenu.style.top=(editMenu.offsetTop+editMenuNormalHeight)+"px";
	}
}

function showSingleAdd(event){
	singleAddPopup.style.left=(event.pageX-singleAddPopup.offsetWidth)+"px";
	singleAddPopup.style.top=event.pageY+"px";
	showPopup(singleAddPopup);
}



function singleTaskAdd(){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(!(request.status==200)){
				alert("database error");
			}else{
				document.location.reload(false);
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=ADD_TASK&projectId="+document.getElementById("singleAddProjectId").value+
			"&team="+document.getElementById("singleAddTeam").value+"&name="+document.getElementById("singleAddName").value+
			"&link="+document.getElementById("singleAddLink").value,true);
	request.send(); 
	closePopup(singleAddPopup);
}
function updateIssue(){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(!(request.status==200)){
				alert("database error");
			}else{
				document.location.reload(true);
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=CREATE_ISSUE&id="+editID.value+"&issueContent="+document.getElementById("issueText").value,true);
	request.send();
	hideIssueEditor();
}
function resolveIssue(){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText=="error"){
				alert("internal server error");
			}else{
				lastImg.src="css/"+request.responseText+".png";
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=RESOLVE_ISSUE&id="+editIssuePopup.dataId+"&name="+editIssuePopup.dataName,true);
	request.send(); 
	closePopup(editIssuePopup);
}


function payForJob(){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText!=""){
				document.getElementById("pay_"+editID.value).innerHTML=request.responseText;
				payButton.style.visibility="hidden";
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=PAY_FOR_JOB&id="+editID.value+"&paid=yes",true);
	request.send();
}

function unpaid(id){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText!=""){
				document.getElementById("pay_"+id).innerHTML="";
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=UNPAYD_JOB&id="+id+"&paid=no",true);
	request.send();
}

//deliverInfo
var deliverInfoId=0;
function saveDeliverInfo(){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText!=""){
				alert(error);
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=SAVE_DELIVER_INFO&id="+deliverInfoId+
			"&revision="+document.getElementById("deliverInfoRevision").value+
			"&branch="+document.getElementById("deliverInfoBranch").value,true);
	request.send();
}

function showDeliverInfo(id, event, obj){
	deliverInfoId=id;
	if(obj.src.indexOf("yes",0)>-1){
		var request=new XMLHttpRequest();
		request.onreadystatechange=function(){
			if(request.readyState==4){
				deliverInfo.innerHTML=request.responseText;
			}
		};
		request.open("GET","AJAXBoardHandler?reason=GET_DELIVER_INFO&id="+id,true);
		request.send();
		deliverInfo.style.top=event.pageY+"px";
		deliverInfo.style.left=(event.pageX-deliverInfo.offsetWidth)+"px";
		showPopup(deliverInfo);
	}
}